﻿
//#define ADD_DEBUG_LOG


using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace QRCodeTool
{
    /// <summary>
    /// 
    /// </summary>
    public class CRegEditCtl
    {
        //Log4Class _log4 = new Log4Class("CRegEditCtl");

        /// <summary>
        /// 键值,初始化时，传入什么参数就用什么能数
        /// </summary>
        string keyName = "QRCodeTool";
        /// <summary>
        /// 注册列表中的路径
        /// </summary>
        string regListPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
        /// <summary>
        /// 注册列表路径下的文件夹
        /// </summary>
        string folderName = "Run";
        /// <summary>
        /// 软件所在路径，可改
        /// </summary>
        string valueName = "D:\\NiRen\\NRBackground\\NRBackgroundSoft.exe";

        public CRegEditCtl()
        {

        }

        public CRegEditCtl(string valName)
        {
            string dbpath = GetDBPath(keyName);
            if (dbpath != null && dbpath != "") {
#if ADD_DEBUG_LOG
                this._log4.PrintfLog(Log4Class.DEBUG, "数据库路径已存在注册列表中！Path=" + dbpath);
#endif
                return;
            }
            CreateKey(keyName);
            WriteRegedit(keyName, keyName, valName);
#if ADD_DEBUG_LOG
            this._log4.PrintfLog(Log4Class.DEBUG, "数据库路径写入注册列表完成！");
#endif
        }

        /// <summary>
        /// 构造函数，已经关联了注册表的判断与执行函数
        /// </summary>
        /// <param name="keyName">键值名，即写入注册列表的键标志</param>
        /// <param name="valueName">运行文件所在路径，包括文件名</param>
        public CRegEditCtl(string keyName, string valueName)
        {
            this.keyName = keyName;
            this.valueName = valueName;

            this.WriteRegList(this.keyName, this.valueName);
        }

        /// <summary>
        /// 构造函数，已经关联了注册表的判断与执行函数
        /// </summary>
        /// <param name="keyName">键值名，即写入注册列表中的键标志</param>
        /// <param name="regListPath">所在注册表中的路径</param>
        /// <param name="folderName">注册表路径下的文件夹</param>
        /// <param name="valueName">运行文件所在路径，包括文件名</param>
        public CRegEditCtl(string keyName, string regListPath, string folderName, string valueName)
        {
            this.keyName = keyName;
            this.regListPath = regListPath;
            this.folderName = folderName;
            this.valueName = valueName;

            this.WriteRegList(this.keyName, this.valueName);
        }
        public string GetDBPath(string keyName)
        {
            string registData = "";
            RegistryKey hkml = Registry.CurrentUser;
            RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\" + keyName, true);
            try
            {
                registData = software.GetValue(keyName).ToString();
            }
            catch { }

            return registData;
        }

        public void CreateKey(string keyName)
        {
            RegistryKey key = Registry.CurrentUser;
            RegistryKey software = key.CreateSubKey(@"SOFTWARE\" + keyName);
#if ADD_DEBUG_LOG
            if(software != null)
                this._log4.PrintfLog(Log4Class.DEBUG, "创建：" + keyName + " 文件夹成功！");
#endif
        }

        public void WriteRegedit(string keyName, string name, string tovalue)
        {
            RegistryKey hklm = Registry.CurrentUser;
            RegistryKey software = hklm.OpenSubKey(@"SOFTWARE\" + keyName, true);
            software.SetValue(name, tovalue);
        }

        public object ReadRegedit(string keyName, string name)
        {
            RegistryKey hklm = Registry.CurrentUser;
            RegistryKey software = hklm.OpenSubKey(@"SOFTWARE\" + keyName, true);
            if (software == null) return null;
            return software.GetValue(name);
        }

        public void DeleteKey(string keyName)
        {
            RegistryKey key = Registry.CurrentUser;
            try
            {
                key.DeleteSubKey(@"SOFTWARE\" + keyName);
            }
            catch { }
        }

        /// <summary>
        /// 写注册列表
        /// </summary>
        /// <param name="regKey">key值</param>
        /// <param name="regValue">value值</param>
        /// <returns></returns>
        public bool WriteRegList(string regKey, string regValue)
        {
            if (GetRegistData(regKey) != "")
            {
                return true;
            }

            //不存在
            WTRegedit(regKey, regValue);

            if (GetRegistData(regKey) != "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        

        //读取指定名称的注册表的值
        public string GetRegistData(string name)
        {
            string registData = "";
            //RegistryKey hkml = Registry.LocalMachine;
            RegistryKey hkml = Registry.CurrentUser;
            RegistryKey software = hkml.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.OpenSubKey(folderName, true);
            try
            {
                registData = aimdir.GetValue(name).ToString();
            }
            catch { }

            return registData;
        }

        ///向注册表中写数据 
        public void WTRegedit(string name, string tovalue)
        {
            //RegistryKey hklm = Registry.LocalMachine;
            RegistryKey hklm = Registry.CurrentUser;
            RegistryKey software = hklm.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.CreateSubKey(folderName);
            aimdir.SetValue(name, tovalue);
        }

        ///删除注册表中指定的注册表项 
        public void DeleteRegist(string name)
        {
            string[] aimnames;
            //RegistryKey hkml = Registry.LocalMachine;
            RegistryKey hkml = Registry.CurrentUser;
            RegistryKey software = hkml.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.OpenSubKey(folderName, true);
            aimnames = aimdir.GetSubKeyNames();
            foreach (string aimKey in aimnames)
            {
                if (aimKey == name)
                    aimdir.DeleteSubKeyTree(name);
            }
        }

        //判断指定注册表项是否存在 ,这个函数有问题，所以用 GetRegistData 函数是否能读出键名来代替
        public bool IsRegeditExit(string name)
        {
            bool _exit = false;
            string[] subkeyNames;
            //RegistryKey hkml = Registry.LocalMachine;
            RegistryKey hkml = Registry.CurrentUser;
            RegistryKey software = hkml.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.OpenSubKey(folderName, true);
            subkeyNames = aimdir.GetSubKeyNames();
            Console.WriteLine("读出的数组长度：" + subkeyNames.Length);
            foreach (string key in subkeyNames)
            {
                Console.WriteLine("在判断函数里打印读到的键值:" + key);
                if (key == name)
                {
                    _exit = true;
                    return _exit;
                }
            }
            return _exit;
        }
    }
}
